/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TipoTarifaView.java
 *
 * Created on 13/11/2009, 09:52:42 AM
 */

package solucionessgh.TipoTarifa;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import solucionessgh.Util.Reporte;

/**
 *
 * @author roderick
 */
public class TipoTarifaView extends javax.swing.JDialog {

    private TipoTarifaModel model;
    private int id;
    private int action;//1=nuevo,2=modificar o eliminar

    public TipoTarifaView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        model = new TipoTarifaModel();
        setResizable(false);
        txt_ttaabr.setEditable(false);
        txt_ttadesc.setEditable(false);
        cbx_ttapred.setEnabled(false);

        btn_nuevo.setEnabled(true);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);

        java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
        java.awt.Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btn_cancelar = new javax.swing.JButton();
        btn_salir = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txt_ttadesc = new javax.swing.JTextField();
        txt_ttaabr = new javax.swing.JTextField();
        btn_buscar = new javax.swing.JButton();
        btn_print = new javax.swing.JButton();
        cbx_ttapred = new javax.swing.JCheckBox();
        btn_nuevo = new javax.swing.JButton();
        btn_editar = new javax.swing.JButton();
        btn_grabar = new javax.swing.JButton();
        btn_eliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tipo de Tarifa");

        btn_cancelar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/close.png"))); // NOI18N
        btn_cancelar.setText("Cancelar");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        btn_salir.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/exit.png"))); // NOI18N
        btn_salir.setText("Salir");
        btn_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salirActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(" Datos Tipo de Tarifa"));

        jLabel2.setText("Descripcion(20) :");

        jLabel3.setText("Abreviatura(3) :");

        txt_ttadesc.setToolTipText("Ingrese descripcion");
        txt_ttadesc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_ttadescKeyTyped(evt);
            }
        });

        txt_ttaabr.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txt_ttaabrPropertyChange(evt);
            }
        });
        txt_ttaabr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_ttaabrKeyTyped(evt);
            }
        });

        btn_buscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/find.png"))); // NOI18N
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });

        btn_print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/print.png"))); // NOI18N
        btn_print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_printActionPerformed(evt);
            }
        });

        cbx_ttapred.setText("Activo :");
        cbx_ttapred.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        cbx_ttapred.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbx_ttapred)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_ttaabr, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(txt_ttadesc, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btn_buscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_print)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_print)
                    .addComponent(btn_buscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_ttadesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_ttaabr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addComponent(cbx_ttapred)
                .addGap(42, 42, 42))
        );

        btn_nuevo.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_nuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/new.png"))); // NOI18N
        btn_nuevo.setText("Nuevo");
        btn_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_nuevoActionPerformed(evt);
            }
        });

        btn_editar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_editar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/edit.png"))); // NOI18N
        btn_editar.setText("Editar");
        btn_editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_editarActionPerformed(evt);
            }
        });

        btn_grabar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_grabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/save.png"))); // NOI18N
        btn_grabar.setText("Grabar");
        btn_grabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_grabarActionPerformed(evt);
            }
        });

        btn_eliminar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_eliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/delete.png"))); // NOI18N
        btn_eliminar.setText("Eliminar");
        btn_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_eliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btn_salir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_eliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_grabar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_editar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_nuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_nuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_editar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_grabar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_eliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_cancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_salir)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        txt_ttadesc.setText("");
        txt_ttaabr.setText("");
        cbx_ttapred.setSelected(false);

        btn_nuevo.setEnabled(true);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);
}//GEN-LAST:event_btn_cancelarActionPerformed

    private void btn_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salirActionPerformed
        setVisible(true);
        dispose();
}//GEN-LAST:event_btn_salirActionPerformed

    private void txt_ttadescKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_ttadescKeyTyped
        String abr = txt_ttadesc.getText();
        if(abr.length()>19){
            txt_ttadesc.setText(abr.substring(0, 19));
        }
}//GEN-LAST:event_txt_ttadescKeyTyped

    private void txt_ttaabrPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txt_ttaabrPropertyChange

}//GEN-LAST:event_txt_ttaabrPropertyChange

    private void txt_ttaabrKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_ttaabrKeyTyped
        String abr = txt_ttaabr.getText();
        if(abr.length()>2){
            txt_ttaabr.setText(abr.substring(0, 2));
        }
}//GEN-LAST:event_txt_ttaabrKeyTyped

    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed
        BuscarView dialog = new BuscarView(new javax.swing.JFrame(), true, model);
        dialog.setVisible(true);
        if(dialog.getId()==0)
            return;

        ResultSet rs = this.model.finder(dialog.getId());
        try {
            if(rs.next()){
                //, , ,
                id = rs.getInt("ttacod");
                txt_ttadesc.setText(rs.getString("ttadesc"));
                txt_ttaabr.setText(rs.getString("ttaabr"));
                cbx_ttapred.setSelected(rs.getBoolean("ttapred"));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        }

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(true);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);
}//GEN-LAST:event_btn_buscarActionPerformed

    private void btn_printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_printActionPerformed
        Reporte rep = new Reporte("/solucionessgh/TipoTarifa/ReporteTipoTarifa.jasper");
        try {
            rep.setVisible();
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.toString());
        }
}//GEN-LAST:event_btn_printActionPerformed

    private void btn_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_nuevoActionPerformed
        txt_ttadesc.setEditable(true);
        txt_ttaabr.setEditable(true);
        cbx_ttapred.setEnabled(true);
        txt_ttadesc.requestFocusInWindow();

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(true);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(true);

        id=0;
        action=1;
}//GEN-LAST:event_btn_nuevoActionPerformed

    private void btn_editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_editarActionPerformed
        action=2;

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(true);
        btn_eliminar.setEnabled(true);
        btn_cancelar.setEnabled(true);

        txt_ttadesc.setEditable(true);
        txt_ttaabr.setEditable(true);
        
        cbx_ttapred.setEnabled(true);
        txt_ttadesc.requestFocusInWindow();
}//GEN-LAST:event_btn_editarActionPerformed

    private void btn_grabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_grabarActionPerformed
        model.setTtaabr(txt_ttaabr.getText());
        model.setTtadesc(txt_ttadesc.getText());
        model.setTtapred(cbx_ttapred.isSelected());
        model.setId(id);

        if(this.action==1){//grabar
            if(model.create()){
                JOptionPane.showMessageDialog(null,
                        "Grabacion OK",
                        "Tipo Tarifa",
                        JOptionPane.INFORMATION_MESSAGE);
                txt_ttadesc.setText("");
                txt_ttaabr.setText("");                
                cbx_ttapred.setSelected(false);

                btn_nuevo.setEnabled(true);
                btn_editar.setEnabled(false);
                btn_grabar.setEnabled(false);
                btn_eliminar.setEnabled(false);
                btn_cancelar.setEnabled(false);
                return;
            }
        }
        if(this.action==2){//modificar
            int n = JOptionPane.showConfirmDialog(
                    null,
                    "Desea Mofiicar ",
                    "Tipo Tarifa",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                if(model.update()){
                    if (n == JOptionPane.YES_OPTION) {
                        JOptionPane.showMessageDialog(null,
                                "Modificacion OK",
                                "Tipo Tarifa",
                                JOptionPane.INFORMATION_MESSAGE);
                        txt_ttadesc.setText("");
                        txt_ttaabr.setText("");
                        cbx_ttapred.setSelected(false);

                        btn_nuevo.setEnabled(true);
                        btn_editar.setEnabled(false);
                        btn_grabar.setEnabled(false);
                        btn_eliminar.setEnabled(false);
                        btn_cancelar.setEnabled(false);
                        return;
                    }
                }
            }
        }
}//GEN-LAST:event_btn_grabarActionPerformed

    private void btn_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_eliminarActionPerformed
        int n = JOptionPane.showConfirmDialog(
                    null,
                    "Desea Eliminar ",
                    "Tipo Tarifa",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                model.setId(id);
                model.delete();
                txt_ttadesc.setText("");
                txt_ttaabr.setText("");
                cbx_ttapred.setSelected(false);
            }
    }//GEN-LAST:event_btn_eliminarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TipoTarifaView dialog = new TipoTarifaView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JButton btn_editar;
    private javax.swing.JButton btn_eliminar;
    private javax.swing.JButton btn_grabar;
    private javax.swing.JButton btn_nuevo;
    private javax.swing.JButton btn_print;
    private javax.swing.JButton btn_salir;
    private javax.swing.JCheckBox cbx_ttapred;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txt_ttaabr;
    private javax.swing.JTextField txt_ttadesc;
    // End of variables declaration//GEN-END:variables

}
